﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThuVienCT;

namespace QLCTNA
{
    public partial class Frm_QuanLyCongThucNauAn : Form
    {
        private ConnectionData cnn=null;
        public Frm_QuanLyCongThucNauAn()
        {
            InitializeComponent();
            FileText ft = new FileText("Config\\Config.ini");

            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            cnn = Connection.Connect();
        }

        private void tbptaomoi_Click(object sender, EventArgs e)
        {

        }

        private void lbltenct_Click(object sender, EventArgs e)
        {

        }

        private void lblphanloai_Click(object sender, EventArgs e)
        {

        }

        private void lblnguyenlieu_Click(object sender, EventArgs e)
        {

        }

        private void lblquytrinh_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btntaomoi_Click(object sender, EventArgs e)
        {

            if (txtmact.Text == "" && txttenct.Text == "" && txtphanloai.Text == "")
            {
                MessageBox.Show("bạn phải nhập đầy đủ thông tin", "WARN!!!!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtmact.Focus();
               
            }
            else
            {
                string insert = null;
                int check = 0;
                if (cnn.Check())
                {
                    insert = " insert into vw_CongThucNauan([Mã Công Thức],[Tên Công Thức],[Phân Loại],[Nguyên Liệu],[Quy Trình])" + "values({0},{1},{2},{3},{4});";
                    check = cnn.Execute(insert, txtmact.Text, txttenct.Text, txtphanloai.Text, txtnguyenlieu.Text, txtquytrinh.Text);
                    if (check > 0)
                    {
                        MessageBox.Show("Đã tạo mới công thức nấu ăn", "thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        clean_taomoi();
                    }
                    else
                    {
                        MessageBox.Show("khong them thanh cong", "thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void clean_taomoi()
        {
            txtmact.Text = "";
            txttenct.Text = "";
            txtnguyenlieu.Text = "";
            txtphanloai.Text = "";
            txtquytrinh.Text = "";
        }

        private void lblmact_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private string str = "---Sinh viên thực hiện Phạm Ngọc Cường-Trần Văn Tài-Lớp 54PM1---";
        int i = 0;
        private string str2;

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (i < str.Length)
            {
                str2 += str[i];
                lblduoi.Text = str2;
                lblduoi.RightToLeft += 2;
                i++;
            }
            else
            {
                i = 0;
                str2 = "";
            }
        }

        private void tbphienthi_Click(object sender, EventArgs e)
        {
            
        }

        private void Frm_QuanLyCongThucNauAn_Load(object sender, EventArgs e)
        {
            string selection = null;
           
            if (cnn.Check())
            {
                selection = "select * from vw_congthucnauan";
                dgvhienthi.DataSource = cnn.SelectTable(selection);
               
                
                clocktab2();
            }
           
        }
        private void clocktab1()
        {
            txtmact.Enabled = false;
            txttenct.Enabled = false;
            txtnguyenlieu.Enabled = false;
            txtphanloai.Enabled = false;
            txtquytrinh.Enabled = false;
        }
        private void clocktab2()
        {
            txtma.Enabled = false;
            txtten.Enabled = false;
            txtpl.Enabled = false;
            txtqt.Enabled = false;
            txtnl.Enabled = false;
        }
        private void unclocktab1()
        {
            txtmact.Enabled = true;
            txttenct.Enabled = true;
            txtnguyenlieu.Enabled = true;
            txtphanloai.Enabled = true;
            txtquytrinh.Enabled = true;
        }
        private void unclocktab2()
        {
            txtma.Enabled = true;
            txtten.Enabled = true;
            txtpl.Enabled = true;
            txtqt.Enabled = true;
            txtnl.Enabled = true;
        }
        private void locktext()
        {
            //  txtma.Enabled = false;
            //txtten.Enabled = false;
            //  txtpl.Enabled = false;
            btnluu.Enabled = true;
            btnsua.Enabled = false;
        }

        private void un_locktext()
        {
            txtma.Enabled = true;
            txtten.Enabled = true;
            txtpl.Enabled = true;
            btnsua.Enabled = false;
            btnluu.Enabled = true;
        }
     
        private void dgvhienthi_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            unclocktab2();
            btnsua.Enabled = true;
            int dong;
            dong = e.RowIndex;
            txtma.Text = dgvhienthi.Rows[dong].Cells[0].Value.ToString();
            txtten.Text = dgvhienthi.Rows[dong].Cells[1].Value.ToString();
            txtpl.Text = dgvhienthi.Rows[dong].Cells[2].Value.ToString();
            txtnl.Text = dgvhienthi.Rows[dong].Cells[3].Value.ToString();
            txtqt.Text = dgvhienthi.Rows[dong].Cells[4].Value.ToString();
            
        }

        private void button5_Click(object sender, EventArgs e)
        {

            if (dgvhienthi.Rows.Count > 0 && !string.IsNullOrEmpty(txtma.Text))
            {

                if (MessageBox.Show("ban co muon xem chi tiet khong?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {


                    {
                        Frm_chitiet ct = new Frm_chitiet();
                        Crpchitiet rpt = new Crpchitiet();

                        // DataTable dt = cnn.SelectTable("select * from vw_congthucnauan where ma='" + lblma.Text + "'");

                        DataTable dt = cnn.SelectTable("select * from vw_CongThucNauAn where [Mã công thức]='" + txtma.Text + "'");
                        rpt.SetDataSource(dt);
                        ct.crtviewer.ReportSource = rpt;
                        ct.ShowDialog();

                    }
                }

                else
                {
                    MessageBox.Show("ban chua chon cong thuc de xem");
                }
            }
            else
            {
                MessageBox.Show("ban chua chon cong thuc cu the");
            }
            
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (txtten.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn thông tin để sửa");

            }
            else
            {
                btnxemchitiet.Enabled = false;
                btnsua.Enabled = false;
                btnluu.Enabled = true;
                txtma.Enabled = false;
                txtten.Focus();
            }
        }

        private void btnluu_Click(object sender, EventArgs e)
        {

        }

        private void btnluu_Click_1(object sender, EventArgs e)
        {
          
        }

        private void txtluu_Click(object sender, EventArgs e)
        {
            int kt = 0;
            {
                string update = null;
               update = " update vw_CongthucNauAn set [Tên Công Thức]='"+txtten.Text+"', [Phân Loại] = '"+txtpl.Text+"' ,[Nguyên Liệu]='"+txtnl.Text+"', [Quy Trình]='"+txtqt.Text+"'"
                    + "where [Mã Công Thức] like '" + txtma.Text + "'";
                kt = cnn.Execute(update);
                dgvhienthi.DataSource = cnn.SelectTable("select * from vw_CongThucNauAn");

                if (kt > 0)
                {
                    MessageBox.Show("Đã lưu thành công!");
                    btnsua.Enabled = true;
                    btnluu.Enabled = false;
                    btnxemchitiet.Enabled = true;

                    dgvhienthi.DataSource = cnn.SelectTable("select * from vw_CongThucNauan");

                }
                else
                {
                    MessageBox.Show("khong thanh cong");
                }
       


            }
        }

    }
}
